import pygame

# Identical items in the Inventory get stored together, and a count of them kept, 
# so the top is not necessarily where an item will be added to
class Inventory:
    def __init__(self, size=-1):
        self.size = size # maximum number of items, -1 means infinite
        self.top = 0
        self.items = list()
        self.font = pygame.font.SysFont(pygame.font.get_default_font(), 14)
    
    #add an item
    def add(self, item, quantity = 1):
        # if adding would reach size limit
        if(self.size != -1 and (len(self.items) + quantity) > self.size):
            raise ValueError, "Cannot add more items"
        
        # check if we already have item type
        for inv_item in self.items:
            if inv_item['item'] == item:
                inv_item['quantity'] += quantity
                return
        
        # by now, we know item type isn't in inventory
        inv_item = {'item': item, 'quantity': quantity}
        self.items.insert(self.top, inv_item)
    
    #return the top item
    def get(self):
        return self.items[self.top]['item']
        
    def get_quantity(self):
        return self.items[self.top]['quantity']
    
    #remove the top item
    def remove(self, quantity = 1):
        self.items[self.top]['quantity'] -= quantity
        # if removing the number would make the quantity non-negative, remove the item
        if self.items[self.top]['quantity'] <= 0:
            self.items.remove(self.items[self.top])
            self.next()

    # set the next item to be top
    def next(self):
        if(len(self.items) == 0):
            self.top = 0
        else:
            self.top += 1
            self.top %= len(self.items)
    
    # set the previous item to be top
    def previous(self):
        if(len(self.items) == 0):
            self.top = 0
        else:
            self.top -= 1
            self.top %= len(self.items)
            
    def get_image(self):
        item_surface = self.get().get_image()
        
        # add number iff there is more than 1 of an item
        if self.get_quantity() == 1:
            return item_surface
        else:
            # copy item so that the altered one is not cached
            inven_surface = item_surface.copy()
            
            # create number and lay over item image
            quantity_surface = self.font.render(str(self.get_quantity()), True, (255, 255, 255))        

            # put on the bottom right side
            right_side = inven_surface.get_width() - quantity_surface.get_width()
            bottom_side = inven_surface.get_height() - quantity_surface.get_height()            
            inven_surface.blit(quantity_surface, (right_side, bottom_side))
            
            return inven_surface

    def __iter__(self):
        return self.items.__iter__()
        
    def __next__(self):
        return self.items.__next__()
        
    def __contains__(self, item):
        return item in self.items
        
    def __len__(self):
        return len(self.items)
        
    def __str__(self):
        if len(self.items) == 0:
            return ""
        else:
            return str(self.get())